package com.niit.blog.controller;

import com.niit.blog.entity.domain.BlogCategory;
import com.niit.blog.entity.domain.BlogLink;
import com.niit.blog.service.CategoryService;
import com.niit.blog.service.LinkService;
import com.niit.blog.util.PageQueryUtil;
import com.niit.blog.util.Result;
import com.niit.blog.util.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class LinkController {
    @Autowired
    private LinkService linkService;
    @GetMapping("/links")
    public String linkPage(HttpServletRequest request) {
        request.setAttribute("path", "links");
        return "user/link";
    }
    @RequestMapping(value = "/links/list", method = RequestMethod.GET)
    @ResponseBody
    public Result list(@RequestParam Map<String, Object> params) {
        if (StringUtils.isEmpty(params.get("page")) || StringUtils.isEmpty(params.get("limit"))) {
            return ResultGenerator.genFailResult("参数异常！");
        }
        PageQueryUtil pageUtil = new PageQueryUtil(params);
        return ResultGenerator.genSuccessResult(linkService.getBlogLinkPage(pageUtil));
    }

    @RequestMapping(value = "/links/save", method = RequestMethod.POST)
    @ResponseBody
    public Result save(@RequestParam("linkId") Integer linkId,
                       @RequestParam("linkName") String linkName,
                       @RequestParam("linkUrl") String linkUrl,
                       @RequestParam("linkDescription") String linkDescription,
                       @RequestParam("linkRank") int linkRank) {
        BlogLink link = new BlogLink();
        link.setLinkId(linkId);
        link.setLinkName(linkName);
        link.setLinkUrl(linkUrl);
        link.setLinkDescription(linkDescription);
        link.setLinkRank(linkRank);
        link.setCreateTime(new Date());
        link.setIsDeleted((byte) 0);
        // Save the BlogLink object using the service
        if (linkService.save(link)) {
            return ResultGenerator.genSuccessResult(200);
        } else {
            return ResultGenerator.genFailResult("保存链接失败，请重试！");
        }
    }
    @GetMapping("/links/info/{id}")
    @ResponseBody
    public Result info(@PathVariable("id") Integer id) {
        BlogLink Link = linkService.selectById(id);
        return ResultGenerator.genSuccessResult(Link);
    }
/**修改**/
    @RequestMapping(value = "/links/update", method = RequestMethod.POST)
    @ResponseBody
    public Result update(@RequestParam("linkId") Integer linkId,
            @RequestParam("linkName") String linkName,
                         @RequestParam("linkUrl") String linkUrl,
                         @RequestParam("linkDescription") String linkDescription,
                         @RequestParam("linkRank") int linkRank) {
        if (StringUtils.isEmpty(linkName)) {
            return ResultGenerator.genFailResult("请输入链接名称！");
        }
        if (StringUtils.isEmpty(linkUrl)) {
            return ResultGenerator.genFailResult("请输入链接地址！");
        }
        if (StringUtils.isEmpty(linkDescription)) {
            return ResultGenerator.genFailResult("请输入链接描述！");
        }
        if (linkRank < 0) {
            return ResultGenerator.genFailResult("请输入有效的排序值！");
        }
        BlogLink tempLink = new BlogLink();
        tempLink.setLinkId(linkId);
        tempLink.setLinkName(linkName);
        tempLink.setLinkUrl(linkUrl);
        tempLink.setLinkDescription(linkDescription);
        tempLink.setLinkRank(linkRank);
        tempLink.setCreateTime(new Date());
        tempLink.setIsDeleted((byte) 0);
        if (linkService.update(tempLink)) {
            return ResultGenerator.genSuccessResult(200);
        } else {
            return ResultGenerator.genFailResult("链接名称重复");
        }
    }
    @RequestMapping(value = "/links/delete", method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@RequestBody Integer[] ids) {
        if (ids.length < 1) {
            return ResultGenerator.genFailResult("参数异常！");
        }
        if (linkService.deleteBatch(ids)) {
            return ResultGenerator.genSuccessResult(200);
        } else {
            return ResultGenerator.genFailResult("删除失败");
        }
    }

}

